package com.lbh.cloudgallery.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lbh.cloudgallery.entity.AuthorEntity;
import com.lbh.cloudgallery.entity.ProductEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxx.common.utils.PageUtils;
import com.xxx.common.utils.Query;

import com.lbh.cloudgallery.dao.MuseumDao;
import com.lbh.cloudgallery.entity.MuseumEntity;
import com.lbh.cloudgallery.service.MuseumService;


@Service("museumService")
public class MuseumServiceImpl extends ServiceImpl<MuseumDao, MuseumEntity> implements MuseumService {

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<MuseumEntity> wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)){
            wrapper.eq("id",key)
                    .or().like("name",key);
        }
        IPage<MuseumEntity> page = this.page(
                new Query<MuseumEntity>().getPage(params),
                wrapper
        );

        return new PageUtils(page);
    }

    @Override
    public Map<String, Object> getMuseumList(Page<MuseumEntity> pageMuseum) {

        QueryWrapper<MuseumEntity> wrapper=new QueryWrapper<>();
        wrapper.orderByAsc("id");
        //把分页数据封装到pageAuthor对象
        baseMapper.selectPage(pageMuseum,wrapper);

        List<MuseumEntity> records=pageMuseum.getRecords();
        long current=pageMuseum.getCurrent();
        long pages=pageMuseum.getPages();
        long size=pageMuseum.getSize();
        long total=pageMuseum.getTotal();

        boolean hasNext=pageMuseum.hasNext();
        boolean hasPrevious=pageMuseum.hasPrevious();

        //将分页数据获取出来，放到mp集合
        Map<String,Object> map=new HashMap<>();
        map.put("items",records);
        map.put("current",current);
        map.put("pages",pages);
        map.put("size",size);
        map.put("total",total);
        map.put("hasNext",hasNext);
        map.put("hasPrevious",hasPrevious);

        return map;
    }

}